<?php
namespace models;

class tasks
{



    //  取某老师的上架课程
    public function getTeachingTasks()
    {
        $tid = GS('auth')['id'];
        $r = db('tasks')->join(' as a left join tb_categorys as b on a.category_id = b.id')
        ->where('a.teacher_id = ? and teaching=1',$tid)
        ->order('category_name,a.name')
        ->fetchAll('a.*,b.name as category_name');
        $x = '';
        $a = array();
        foreach ($r as $v) {
            if ($x !== $v['category_name']) {
                $a[$v['category_name']][0] = array('id' => $v['id'], 'name' => $v['name'], 'category_id' => $v['category_id']);
                $x                         = $v['category_name'];
            } else {
                array_push($a[$v['category_name']], array('id' => $v['id'], 'name' => $v['name'], 'category_id' => $v['category_id']));
            }
        }
        return $a;
    }


    // 取某分类下所有课程
    public function getByCatID($category_id)
    {
        return db('tasks')->order('name')->where('category_id=?',$category_id)->fetchAll();

    }

    // 取指定ID的课程
    public function getByID($ID){
        return db('tasks')->where('id=?',$ID)->fetch();
    }

}
